package com.allwees.bs.module.oms.req.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.core.modelbase.req.QueryReq;
import com.allwees.bs.module.oms.constant.EOmsOrderItemQueryStatus;
import com.allwees.bs.module.oms.constant.EOmsOrderItemQueryType;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.core.common.constant.EStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 *
 *
 * @author yangylsky
 * @since 2020-01-09
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class OmsOrderItemQueryReq extends QueryReq<OmsOrderItem> {

	private String orderNo;

	private String orderUuid;

	private String expressCode;

    private Date createdAtFrom;

    private Date createdAtTo;

    private Date settledAtFrom;

    private Date settledAtTo;

    private EStatus status;

    private String defNo;

    private String userUuid;

	private EOmsOrderItemQueryType queryType;

	private EOmsOrderItemQueryStatus state;


	private Date shippingAtFrom;

	private Date shippingAtTo;

	private String retailerUuid;

	@Override
	public void buildQuery(QueryWrapper<OmsOrderItem> queryWrapper) {
		queryWrapper
			.eq(StringUtils.isNotBlank(orderNo), "order_no", orderNo)
			.eq(StringUtils.isNotBlank(orderUuid), "order_uuid", orderUuid)
			.eq(StringUtils.isNotBlank(expressCode),"express_code",expressCode)
			.eq(StringUtils.isNotBlank(defNo),"def_no",defNo)
			.ge(createdAtFrom != null, "created_at", createdAtFrom)
			.le(createdAtTo != null, "created_at", createdAtTo)
			.ge(shippingAtFrom != null, "shipping_at", shippingAtFrom)
			.le(shippingAtTo != null, "shipping_at", shippingAtTo)
			.ge(settledAtFrom != null, "settled_at", settledAtFrom)
			.le(settledAtTo != null, "settled_at", settledAtTo);
		if (status != null) {
			queryWrapper.eq("status", status.value());
		} else {
			if(queryType != null){
				queryWrapper.in("status", queryType.getStatus());
			} else {
				if (state != null) {
					queryWrapper.in("status", state.getStatus());
				} else {
					queryWrapper.in("status", EOmsOrderItemQueryStatus.ALL.getStatus());
				}
			}

		}

		if (StringUtils.isNotEmpty(userUuid)) {
			queryWrapper.eq("user_uuid", userUuid);
		}
		if (StringUtils.isNotEmpty(retailerUuid)) {
		    queryWrapper.eq("retailer_uuid",retailerUuid);
        }


	}
}
